import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import store from './store.js';
import {Provider} from 'react-redux';
import {addToCart, updateCart, deleteFromCart} from './actions/cart-action.js'
import reportWebVitals from './reportWebVitals';
import App from './containers/app'
import AddCart from './containers/addToCart'
// Action
/* const ADD_TO_CART = 'ADD_TO_CART';
function addToCart(product,quantity,unitCost){
	return {
		type:ADD_TO_CART,
		payload:{product,quantity,unitCost}
	}
}
// end of Action

// 初始化state
import {createStore,combineReducers } from 'redux'
const productReducer = (state= [], action)=>{
	return state;
}
const initialState = {
	cart:[{product: 'bread 700g',quantity: 2,unitCost: 90},
		  {product: 'milk 500ml',quantity: 1, unitCost: 47}]
}
const cartReducer = (state=initialState, action)=>{
	switch(action.type){
		case ADD_TO_CART:{
			return {
				...state,
				cart:[...state.cart,action.payload]
			}
		}
		default:{
			return state;
		}
	}
	return state;
}
const allReucers ={
	products:productReducer,
	shoppingCart:cartReducer
}
const rootReducer = combineReducers(allReucers);
let store = createStore(rootReducer);
*/
let unsubscribe = store.subscribe(() =>
  console.log('store change',store.getState())
);
console.log("initial state: ", store.getState());
store.dispatch(addToCart('Coffee 500gm', 1, 250));
store.dispatch(addToCart('Flour 1kg', 2, 110));
store.dispatch(addToCart('Juice 2L', 1, 250));
store.dispatch(updateCart('Flour 1kg', 1, 55));
store.dispatch(deleteFromCart('Coffee 500gm'));
unsubscribe()
// end of 初始化state


ReactDOM.render(
	<Provider store={store}>
		<div>hello redux</div>	
		<App />
		<AddCart />
    </Provider>,
  document.getElementById('root')
);
reportWebVitals();
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
// reportWebVitals();
